package com.example.ggpicturegg.tool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;

public class MyUtils {
    private static ObjectMapper mapper = new ObjectMapper();

    public MyUtils(){

    }

    /**
     * 将图片转换为Base64格式字符串
     * @param bitmap  图片, Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawableId);
     * @return Base64格式字符串
     */
    public static String BitmapToBase64(Bitmap bitmap) {
        //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawableId);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.WEBP, 100, byteArrayOutputStream);
        byte[] bytes = byteArrayOutputStream.toByteArray();
        byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
        return new String(encode);
    }

    /**
     * 将Base64格式字符串转换为位图
     * @param encodeString 图片的Base64格式字符串
     * @return 位图
     */
    public static Bitmap Base64ToBitmap(String encodeString) {
        byte[] bytes = Base64.decode(encodeString, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }


    /**
     * 把Object转化为json String.   例如把HashMap转化为json String
     * @param object  等转化的对象
     * @return   Json String
     * @usage 例如把HashMap转化为json string
     *      String str_json = object2JsonString(hm);
     */
    public static String object2JsonString(Object object) {
        StringWriter writer = new StringWriter();
        try {
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);  // 允许使用非双引号属性名字
            mapper.writeValue(writer, object);
        } catch (JsonGenerationException e) { e.printStackTrace(); }
        catch (JsonMappingException e) { e.printStackTrace(); }
        catch (IOException e) { e.printStackTrace(); }

        return writer.toString();
    }



    /*
     * desc: 把收到到字符串转换为 HashMap<String,String>
     * @param	jstr   接收到的字符串，如：{"username":"tom","type":"image","content":"abcd"}
     * @return	HashMap对象，结构 HashMap<String, String>
     */
    public static HashMap<String, String> jstr2hm(String jstr){
        HashMap<String, String> hm = new HashMap<String, String>();
        try {
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);  // 允许使用非双引号属性名字
            hm = mapper.readValue(jstr, new TypeReference<HashMap<String, String>>() {});
        } catch (RuntimeException e) { throw e; }
        catch (Exception e) {     e.printStackTrace();    }
        return hm;
    }


    /**
     * 把 Json String转化为对象
     * @param <T>
     * @param json_str
     * @param klass   对象类型， 例如 HashMap.class
     * @return 转化的对象
     *   如果把字符串转化为HashMap,类型为HashMap<String ,Object>
     *       HashMap<String ,Object> hm = jsonString2Object(str_json,HashMap.class);
     */
    public static  <T> T jsonString2Object(String json_str, Class<T> klass) {
        T object;
        try {
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);  // 允许使用非双引号属性名字
            object = mapper.readValue(json_str, klass);
        } catch (RuntimeException e) { throw e; }
        catch (Exception e) {     return null;     }

        return object;
    }

    /*
     * desc: 处理查询得到的数据库记录json字符串，将其转化为 ArrayList<HashMap<String,String>>
     * @param	jstr    数据库记录json字符串，如：[{"id":"1","ch_name":"蝙蝠","en_name":"bat"},{"id":"2","ch_name":"蜜蜂","en_name":"bee"}]
     * @return	Java对象，结构 HashMap<String, HashMap<String, String>>
     */
    public static ArrayList<HashMap<String, String>> jstr2alhm(String jstr){
        ArrayList<HashMap<String, String>> al_hm = new ArrayList<HashMap<String, String>>();
        try {
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);  // 允许使用非双引号属性名字
            al_hm = mapper.readValue(jstr, new TypeReference<ArrayList<HashMap<String, String>>>() {});
        } catch (RuntimeException e) { throw e; }
        catch (Exception e) {     e.printStackTrace();    }
        return al_hm;
    }

}